/*
 * OrderModel.java
 * business
 *
 * Created by ChenTao on 2017/3/2.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.data.model;

import android.support.v4.util.ArrayMap;
import android.text.TextUtils;

import com.yidiandao.business.R;
import com.yidiandao.business.constant.Common;
import com.yidiandao.business.constant.Urls;
import com.yidiandao.business.data.bean.AfterSaleBean;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.bean.Order;
import com.yidiandao.business.data.bean.Shop;
import com.yidiandao.business.data.net.BaseRequest;
import com.yidiandao.business.data.net.BaseResponse;
import com.yidiandao.business.data.net.NetBiz;
import com.yidiandao.business.data.net.NetListener;
import com.yidiandao.business.data.net.exception.BizErrorInfo;
import com.yidiandao.business.data.net.exception.NetErrorInfo;
import com.yidiandao.business.data.request.AfterSaleRequest;
import com.yidiandao.business.data.request.BuyAgainRequest;
import com.yidiandao.business.data.request.OrderRequest;
import com.yidiandao.business.data.request.ZXingResultRequest;
import com.yidiandao.business.data.response.AfterSaleProgressResponse;
import com.yidiandao.business.data.response.AfterSaleResponse;
import com.yidiandao.business.data.response.BuyAgainResponse;
import com.yidiandao.business.data.response.LogisticsResponse;
import com.yidiandao.business.data.response.OrderDetailResponse;
import com.yidiandao.business.data.response.OrderResponse;
import com.yidiandao.business.data.response.PayOrderResponse;
import com.yidiandao.business.data.response.RecommendGoodsListResponse;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.AgentTitleListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.ExchangeListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.LogisticsFooterListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.LogisticsHeaderListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.LogisticsListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.OrderCertificateInfoListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.OrderDetailAddressListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.OrderDetailGoodsInfoListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.OrderDetailMessageListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.OrderDetailPayInfoListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.OrderDetailProgressListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.OrderDetailShopActiveListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.OrderListItem;
import com.yidiandao.business.ui.widget.view.AfterSaleInfoLayout;
import com.yidiandao.business.ui.widget.view.AfterSaleItemView;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.TimeUtils;
import com.yidiandao.business.utils.UIUtils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * 我的订单
 */
public class OrderModel implements IOrderModel {

    private NetBiz mNetBiz = new NetBiz();

    @Override
    public void getOrderInfo(
            String tag, final int state, int page, final onAccessNetSuccessListener listener) {
        OrderRequest request = new OrderRequest();
        request.setPage(page);
        request.setPageSize(Common.PAGER_SIZE);
        request.setTag(tag);
        request.setState(state);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.ORDER_LIST);
        mNetBiz.sendGetRequest(request, OrderResponse.class, new NetListener<OrderResponse>() {
            @Override
            public void onSuccess(OrderResponse orderResponse) {
                OrderResponse.Data data = orderResponse.getData();

                int total = orderResponse.getPagination().getTotal();
                List<Order> orderList = data.getOrderList();
                ArrayList<DisplayListItem> displayListItems = new ArrayList<>();
                if (null != orderList) {
                    for (int i = 0; i < orderList.size(); i++) {
                        Order bean = orderList.get(i);
                        displayListItems.add(new OrderListItem(bean));
                    }
                }

                if (listener != null) {
                    listener.onSuccess(displayListItems, total);
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public void getExchangeInfo(String tag, int page, final onAccessNetSuccessListener listener) {
        OrderRequest request = new OrderRequest();
        request.setPage(page);
        request.setPageSize(Common.PAGER_SIZE);
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.ORDER_APPLY_LIST);
        mNetBiz.sendGetRequest(request, OrderResponse.class, new NetListener<OrderResponse>() {
            @Override
            public void onSuccess(OrderResponse orderResponse) {
                OrderResponse.Data data = orderResponse.getData();
                int total = orderResponse.getPagination().getTotal();
                List<Order> orderList = data.getOrderList();

                ArrayList<DisplayListItem> displayListItems = new ArrayList<>();
                if (null != orderList) {
                    for (int i = 0; i < orderList.size(); i++) {
                        Order bean = orderList.get(i);
                        displayListItems.add(new ExchangeListItem(bean));
                    }
                }
                if (listener != null) {
                    listener.onSuccess(displayListItems, total);
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public void getOrderDetailInfo(
            String tag, final String orderID, final onGetOrderDetailSuccessListener listener) {
        OrderRequest request = new OrderRequest();
        request.setTag(tag);
        request.setOrderId(orderID);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.ORDER_INFO);
        mNetBiz.sendGetRequest(request, OrderDetailResponse.class,
                new NetListener<OrderDetailResponse>() {
                    @Override
                    public void onSuccess(OrderDetailResponse orderDetailResponse) {
                        OrderDetailResponse.Data data = orderDetailResponse.getData();

                        List<DisplayListItem> displayListItems = new ArrayList<>();

                        // 订单配送信息
                        displayListItems.add(new OrderDetailProgressListItem(data));
                        // 地址
                        displayListItems.add(new OrderDetailAddressListItem(data));
                        //店铺信息
                        Shop store = data.getStore();
                        if (null != store) {
                            displayListItems.add(new AgentTitleListItem(store));
                        }
                        //商品信息
                        List<Goods> goodsList = data.getGoodsList();
                        if (null != goodsList && goodsList.size() != 0) {
                            for (int i = 0; i < goodsList.size(); i++) {
                                displayListItems.add(
                                        new OrderDetailGoodsInfoListItem(goodsList.get(i)));
                            }
                        }
                        if (null != data.getOrder().getFavourableList()
                                && (data.getOrder().getFavourableList().size() != 0 || null != data.getOrder().getCouponInfo())) {
                            displayListItems.add(
                                    new OrderDetailShopActiveListItem(
                                            data.getOrder()));
                        }
                        // 发票信息
                        displayListItems.add(new OrderDetailMessageListItem(data));
                        // 订单信息
                        displayListItems.add(new OrderDetailPayInfoListItem(data));
                        //凭证信息
                        if(null!=data.getOrder().getPictureList()&&data.getOrder().getPictureList().size()!=0) {
                            displayListItems.add(new OrderCertificateInfoListItem(data.getOrder().getPictureList()));
                        }

                        if (listener != null) {
                            listener.onSuccess(displayListItems, 0, data);
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void cancelOrder(
            String tag, String orderID, String reason, final onAccessNetListener listener) {
        OrderRequest request = new OrderRequest();
        request.setTag(tag);
        request.setOrderId(orderID);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.CANCEL_ORDER);
        request.setReason(reason);

        mNetBiz.sendPostRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse baseResponse) {
                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public void sendZXingResult(String tag, String result, final onAccessNetListener listener) {
        ZXingResultRequest request = new ZXingResultRequest();
        request.setUrl(result);
        request.setTag(tag);
        request.setFromApp(Common.FROM_APP);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));

        mNetBiz.sendGetRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse baseResponse) {
                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public void getAfterSaleInfo(
            String tag, String orderID, final onGetAfterSaleNetListener listener) {
        OrderRequest request = new OrderRequest();
        request.setTag(tag);
        request.setOrderId(orderID);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.APPLY_ORDER);
        mNetBiz.sendGetRequest(request, AfterSaleResponse.class,
                new NetListener<AfterSaleResponse>() {
                    @Override
                    public void onSuccess(AfterSaleResponse afterSaleResponse) {
                        List<Goods> data = afterSaleResponse.getData();
                        if (listener != null) {
                            listener.onSuccess(data);
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public List<AfterSaleBean> sendExchangeInfo(
            AfterSaleInfoLayout infoLayout, String orderId, List<Goods> goodsList) {

        List<AfterSaleBean> goods_list = new ArrayList<>();
        //获取orderId
        for (int i = 0; i < infoLayout.getChildCount(); i++) {
            AfterSaleItemView view = (AfterSaleItemView) infoLayout.getChildAt(i);
            //判断是否选中
            if (view.getIsCheck()) {
                //类型
                String style = view.getStyle();
                //原因
                String desc = view.getDesc();
                //数量
                int number = view.getNumber();
                if (TextUtils.isEmpty(style) || TextUtils.isEmpty(desc)) {
                    UIUtils.showToast(UIUtils.getString(R.string.exchange_after_sale_tip));
                    return null;
                }
                String goodsId = goodsList.get(i).getGoodsId();

                AfterSaleBean goods = new AfterSaleBean();
                goods.setType(style);
                goods.setGoods_id(goodsId);
                goods.setNumber(number);
                goods.setReason(desc);
                goods.setOrder_id(orderId);
                goods_list.add(goods);
            }
        }
        return goods_list;
    }

    @Override
    public void sendAfterSaleInfoToNet(
            String tag, String info, final onAccessNetListener listener) {
        AfterSaleRequest request = new AfterSaleRequest();
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.APPLY_ORDER);
        request.setGood(info);
        mNetBiz.sendPostRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse baseResponse) {
                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public void getAfterSaleProgress(
            String tag, String orderID, final onGetAfterSaleProgressListener listener) {
        OrderRequest request = new OrderRequest();
        request.setTag(tag);
        request.setOrderId(orderID);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.ORDER_APPLY_INFO);
        mNetBiz.sendGetRequest(request, AfterSaleProgressResponse.class,
                new NetListener<AfterSaleProgressResponse>() {
                    @Override
                    public void onSuccess(AfterSaleProgressResponse baseResponse) {
                        AfterSaleProgressResponse.DataBean data = baseResponse.getData();
                        if (listener != null) {
                            listener.onSuccess(data);
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void confirmReceive(String tag, String orderId, final onAccessNetListener listener) {
        OrderRequest request = new OrderRequest();
        request.setTag(tag);
        request.setOrderId(orderId);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.RECEIVE_ORDER);

        mNetBiz.sendPostRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse baseResponse) {
                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public void buyAgain(String orderId, String tag, final OnBuyAgainListener listener) {

        BuyAgainRequest request = new BuyAgainRequest();
        request.setUrl(Urls.ORDER_BUY_AGAIN);
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setOrderId(orderId);

        mNetBiz.sendGetRequest(request, BuyAgainResponse.class,
                new NetListener<BuyAgainResponse>() {
                    @Override
                    public void onSuccess(BuyAgainResponse response) {
                        BuyAgainResponse.Data data = response.getData();
                        if (listener != null) {
                            listener.onSuccess(data.getRecJson());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void getLogisticsInfo(String orderId, String tag, final onGetDataListener listener) {
        BuyAgainRequest request = new BuyAgainRequest();
        request.setUrl(Urls.ORDER_LOG_LIST);
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setOrderId(orderId);
        mNetBiz.sendGetRequest(request, LogisticsResponse.class,
                new NetListener<LogisticsResponse>() {
                    @Override
                    public void onSuccess(LogisticsResponse logisticsResponse) {
                        LogisticsResponse.DataBean data = logisticsResponse.getData();
                        ArrayList<DisplayListItem> displayListItems = new ArrayList<>();
                        if (null != data) {
                            displayListItems.add(new LogisticsHeaderListItem(data));
                            List<LogisticsResponse.DataBean.MsgListBean> msgList =
                                    data.getMsgList();
                            if (null != msgList && msgList.size() != 0) {
                                for (int i = 0; i < msgList.size(); i++) {
                                    LogisticsResponse.DataBean.MsgListBean bean = msgList.get(i);
                                    ArrayMap<String, String> map = new ArrayMap<>();
                                    map.put("info", bean.getMsg());
                                    map.put("date", bean.getDate());
                                    map.put("time", bean.getTime());
                                    map.put("isFirst", i == 0 ? "1" : "0");
                                    map.put("isLast", i == msgList.size() - 1 ? "1" : "0");
                                    displayListItems.add(new LogisticsListItem(map));
                                }
                            }
                            displayListItems.add(new LogisticsFooterListItem(data));
                        }
                        if (listener != null) {
                            listener.onSuccess(displayListItems);
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void loadOrderRecommendGoods(String tag, final OnLoadRecommendGoodsListener listener) {
        BaseRequest request = new BaseRequest();
        request.setUrl(Urls.ORDER_RECOMMEND_GOODS);
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));

        mNetBiz.sendGetRequest(request, RecommendGoodsListResponse.class,
                new NetListener<RecommendGoodsListResponse>() {
                    @Override
                    public void onSuccess(RecommendGoodsListResponse response) {
                        if (listener != null) {
                            listener.onSuccess(response.getData());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return false;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return false;
                    }

                    @Override
                    public void onFinal() {
                    }
                });
    }

    @Override
    public void getUnGetGoodsCount(String tag, final OnLoadUnGetGoodsListener listener) {
        OrderRequest request = new OrderRequest();
        request.setPage(1);
        request.setPageSize(1000);
        request.setTag(tag);
        request.setState(3);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.ORDER_LIST);
        mNetBiz.sendGetRequest(request, OrderResponse.class, new NetListener<OrderResponse>() {
            @Override
            public void onSuccess(OrderResponse orderResponse) {
                int count = 0;
                List<Order> orderList = orderResponse.getData().getOrderList();
                if (orderList == null) {
                    return;
                }
                for (int i = 0; i < orderList.size(); i++) {
                    Order bean = orderList.get(i);
                    String orderTime = bean.getOrderTime();
                    if (null != bean.getButtons() && bean.getButtons().size() != 0) {
                        try {
                            long millis =
                                    TimeUtils.parseMillis(orderTime, "yyyy-MM-dd HH:mm:ss");
                            long curMillis = TimeUtils.getCurMillis();
                            if ((curMillis - millis) / (24 * 3600 * 1000) >= 14) {
                                count++;
                            }
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                }

                if (listener != null) {
                    listener.onSuccess(count);
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                return false;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                return false;
            }

            @Override
            public void onFinal() {
            }
        });
    }

    @Override
    public void onlinePay(String tag, String orderId, final OnLinePayListener listener) {
        OrderRequest request = new OrderRequest();
        request.setTag(tag);
        request.setOrderId(orderId);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.PAY_ORDER);
        mNetBiz.sendGetRequest(request, PayOrderResponse.class,
                new NetListener<PayOrderResponse>() {
                    @Override
                    public void onSuccess(PayOrderResponse response) {
                        String url = response.getData().getUrl();
                        if (listener != null) {
                            listener.onSuccess(url);
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }
}
